package org.demo.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

	@Autowired
    private MessageSource messageSource;

	@RequestMapping("/")
	public String root() {
		return "redirect:/index";
	}
	 @GetMapping(value = "/locale")
	    public String localeHandler(HttpServletRequest request) {
	        String lastUrl = request.getHeader("referer");
	        return "redirect:" + lastUrl;
	    }
	
	@RequestMapping("/index")
	public String index(Model model, Locale locale) {
		model.addAttribute("title", messageSource.getMessage("text.title", null, locale));
		return "index";
	}
	@RequestMapping("/login")
	public String login() {
		return "login";
	}

	@RequestMapping("/login-error")
	public String loginError(Model model) {
		model.addAttribute("loginError", true);
		return "login";
	}

}
